import { getNodeMaxId, getEdgeMaxId } from "@/utils/antvx6/nodeOrEdgeMaxId";
import { NodePoint } from "@/utils//antvx6/nodePoint";
import { edgeLabels, edgeAttrs, edgeRouter } from "@/utils/antvx6/configurationDetails";
/**
 * 点击边插入新的节点，并且生成两条新的边
 * @param { HTMLElement }  elementGraph -- antv x6 元素
 */
export const insertNode = (elementGraph: any) => {
  elementGraph.on("edge:contextmenu", async ({ edge }) => {
    // 处理重复的节点
    const sum: number = getNodeMaxId(elementGraph);
    const ids: string = `n${sum}`;
    const sourceNode = edge.getSourceNode();
    const targetNode = edge.getTargetNode();
    const x = (sourceNode.position().x + targetNode.position().x) / 2;
    const y = (sourceNode.position().y + targetNode.position().y) / 2;
    const paramNode = {
      nodeNameEn: `n${sum}`,
      nodeNameCn: `n${sum}`,
      hasDirection: 0,
      mapNodeType: 9,
      x: x,
      y: y,
    };
    // 添加节点
    const nodeP = new NodePoint();
    nodeP.setData({
      id: ids,
      x,
      y,
      data: {
        x,
        y,
        hasDirection: paramNode.hasDirection,
        mapNodeType: paramNode.mapNodeType,
        nodeNameCn: paramNode.nodeNameCn,
        nodeNameEn: paramNode.nodeNameEn,
      },
    });
    const model = elementGraph.addNode(nodeP.getModel());
    elementGraph.addNode(model);

    // 删除老的边, 添加新边
    const getData = edge.getData();
    const lineMode = getData?.lineMode || 1;
    const edgeType = getData?.edgeType || 1;
    const del = elementGraph.removeEdge(edge);
    const idsEdgeOne: any = del?.id;
    // 创建第一条边
    elementGraph.addEdge({
      id: idsEdgeOne,
      source: sourceNode,
      target: model,
      data: {
        edgeType: edgeType,
        lineMode: lineMode,
        edgeName: idsEdgeOne,
      },
      labels: edgeLabels(idsEdgeOne),
      router: edgeRouter(),
      attrs: edgeAttrs(2, lineMode),
    });
    // 提交第一条边
    submitEdge(elementGraph, idsEdgeOne);
    // 创建第二条边
    const subEdge: number = getEdgeMaxId(elementGraph);
    const idsEdge: string = `e${subEdge}`;
    // 创建第二条边
    elementGraph.addEdge({
      id: idsEdge,
      source: model,
      target: targetNode,
      data: {
        edgeType: edgeType,
        lineMode: lineMode,
        edgeName: idsEdge,
      },
      labels: edgeLabels(idsEdge),
      router: edgeRouter(),
      attrs: edgeAttrs(2, lineMode),
    });
    // 提交第二条边
    submitEdge(elementGraph, idsEdge);
  });
};
/**
 * 提交边
 * @param { HTMLElement }  elementGraph  antv x6 元素
 * @param { string }  edgeId  边Id
 */
export const submitEdge = (elementGraph: any, edgeId: any) => {
  setTimeout(async () => {
    // 获取边的长度
    let lengths: number = 0;
    const edge = elementGraph.getCellById(edgeId);
    const view = elementGraph.findViewByCell(edge);
    const lengthFloor: number = view?.path?.length();
    lengths = Math.floor(lengthFloor * 100) / 100;
    // 起始节点
    const source = edge.getSourceCellId();
    const getCellSource = elementGraph.getCellById(source);
    // 结束节点
    const target = edge.getTargetCellId();
    const getCellTarget = elementGraph.getCellById(target);

    const obj = {
      lengths,
      edgeType: edge.getData()?.edgeType || 1,
      lineMode: edge.getData()?.lineMode || 1,
      startNodeId: getCellSource.getData()?.nodeId,
      endNodeId: getCellTarget.getData()?.nodeId,
      edgeName: edgeId,
    };
    edge.setData(obj);
  }, 100);
};
